﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProductKeyManager.Search;
using System.Globalization;
using System.Drawing;
using System.IO;
using System.Windows.Media.Imaging;
using IconImage = System.Drawing.Icon;

namespace ProductKeyManager.DB
{
    /// <summary>
    /// Products class
    /// </summary>
    public partial class Product
    {
        /// <summary>
        /// Gets or sets the search term.
        /// </summary>
        /// <value>The search term.</value>
        public static string SearchTerm
        {
            get;
            set;
        }

        #region Extensibility Method Definitions
        partial void OnSearchSimilarityChanging(System.Nullable<decimal> value);
        partial void OnSearchSimilarityChanged();
        #endregion

        /// <summary>
        /// Gets the similarity.
        /// </summary>
        /// <param name="search">The search.</param>
        /// <returns></returns>
        public void GetSimilarity(string search)
        {
            SearchSimilarity = search.CalculateSimilarity(this.Name.ToLower(CultureInfo.CurrentCulture));
            //return SearchSimilarity;
        }

        /// <summary>
        /// Zeroes the similarity.
        /// </summary>
        /// <returns></returns>
        public decimal ZeroSimilarity()
        {
            string empty = "";
            SearchSimilarity = empty.CalculateSimilarity(this.Name.ToLower(CultureInfo.CurrentCulture));
            return SearchSimilarity;
        }

        private decimal _SearchSimilarity;

        /// <summary>
        /// Gets or sets the search similarity.
        /// </summary>
        /// <value>The search similarity.</value>
        public decimal SearchSimilarity
        {
            get
            {
                if (!string.IsNullOrEmpty(Product.SearchTerm))
                {
                    this.GetSimilarity(Product.SearchTerm);
                    return _SearchSimilarity;
                }
                return 0m;
            }
            set
            {
                if ((this._SearchSimilarity != value))
                {
                    this.OnSearchSimilarityChanging(value);
                    this.SendPropertyChanging();
                    this._SearchSimilarity = value;
                    this.SendPropertyChanged("SearchSimilarity");
                    this.OnSearchSimilarityChanged();
                }
            }
        }

        /// <summary>
        /// Gets the key count.
        /// </summary>
        /// <value>The key count.</value>
        public int KeyCount
        {
            get
            {
                ProductKeyManagerDataContext db = ProductKeyManagerDataContext.GetContext();
                return (from k in db.Keys where k.ProductId == this.Id select k).Count();
            }
        }

        /// <summary>
        /// Gets the icon URI.
        /// </summary>
        /// <value>The icon URI.</value>
        public Uri IconUri
        {
            get
            {
                return string.IsNullOrEmpty(this.Icon) ? new Uri("Icons/Generic_Application.ico", UriKind.Relative) : new Uri(this.Icon, UriKind.Relative);
            }
        }
    }
}
